home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.11 Nov 96 / DebuggingStarter Code / Debug.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-09  |  4.8 KB  |  157 lines  |  [TEXT/CWIE]

  1. /*________________________________________________________________________________
  2.     Debug.h: general purpose debugging code.
  3. ________________________________________________________________________________*/
  4.  
  5. #pragma once
  6.  
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10.  
  11.  
  12. /*________________________________________________________________________________
  13.     some compilers don't support #pragma mark, which allows nice markers in the
  14.     source file routine name popup menu.
  15. ________________________________________________________________________________*/
  16. #ifdef __MWERKS__
  17.     // CodeWarrior supports this...
  18.     #define PRAGMA_MARK_SUPPORTED    1
  19. #endif
  20.  
  21.  
  22.  
  23.  
  24. /*________________________________________________________________________________
  25.     utility macros 
  26. ________________________________________________________________________________*/
  27. #define IsntNil( p )    ( (p) != nil )
  28. #define IsNil( p )        ( (p) == nil )
  29.  
  30. #define IsntErr( e )    ( (e) == noErr )
  31. #define IsErr( e )        ( (e) != noErr )
  32.  
  33.  
  34.  
  35.  
  36.  
  37. #if DEBUG    // [
  38. /*________________________________________________________________________________
  39.     OSErrString tables
  40. ________________________________________________________________________________*/
  41.  
  42. // a single entry in an error table.  An array of these constitutes a table.
  43. typedef struct DebugOSErrStringTableEntry
  44. {
  45.     OSErr        err;
  46.     const char    *cString;    // a C string
  47. } DebugOSErrStringTableEntry;
  48.  
  49. void    DebugAddOSErrStringTable( const DebugOSErrStringTableEntry * entries, UInt16 numEntries);
  50.     
  51. #endif    // ]
  52.  
  53.  
  54.  
  55.  
  56. /*________________________________________________________________________________
  57.     basic asserts and their variants...
  58. ________________________________________________________________________________*/
  59. #if    DEBUG    // [
  60.  
  61.     void    DebugNumToString( long    num, StringPtr    string);
  62.     
  63.     void    GetDebugNumString( ConstStr255Param msg, long num, StringPtr resultStr);
  64.     
  65.     #define DebugMsg(failStr)                {DebugStr(failStr);}
  66.     #define DebugMsgIf( cond, failStr)        {if ( (cond) ) { DebugMsg(failStr);}}
  67.     #define    DebugNum( failStr, num )        { Str255 msg; GetDebugNumString( failStr, num, msg); DebugStr( msg ); }
  68.                                         
  69.     #define Assert(cond, failStr)            DebugMsgIf( !(cond), failStr)
  70.     
  71. #else    //    ] DEBUG [
  72.  
  73.     #define DebugMsg(failStr)                    {/* nothing */}
  74.     #define DebugMsgIf( cond, failStr)            {/* nothing */}
  75.     #define DebugNum( msg, num )                {/* nothing */}
  76.     #define Assert(cond, failStr)                {/* nothing */}
  77.     
  78. #endif    // ] DEBUG
  79.  
  80.  
  81.  
  82.  
  83. /*________________________________________________________________________________
  84.     more complex asserts
  85. ________________________________________________________________________________*/
  86. #if    DEBUG    // [
  87.  
  88.     void    _AssertHandleIsValid(const void *theHandle, const char *cString, ConstStr255Param optionalMsg);
  89.     void    _AssertAddressIsValidAlign(const void *address, const char *cString, UInt32 align, ConstStr255Param optionalMsg);
  90.     void    AssertNoErr( OSErr err, ConstStr255Param optionalMsg);
  91.  
  92.     #define AssertHandleIsValid(h, msg)                _AssertHandleIsValid( (h), #h, msg)
  93.     #define AssertResourceIsValid(r, msg)            _AssertHandleIsValid( (r), #r, msg)
  94.     #define AssertAddressIsValidAlign(p, a, msg)    _AssertAddressIsValidAlign( (p), #p, a, msg)
  95.     #define AssertAddressIsValid(p, msg)            AssertAddressIsValidAlign( p, 1, msg)
  96.     #define AssertAddressIsValidAlign2( p, msg)        AssertAddressIsValidAlign( p, 2, msg)
  97.     #define AssertAddressIsValidAlign4( p, msg)        AssertAddressIsValidAlign( p, 4, msg)
  98.  
  99. #else    // ] DEBUG [
  100.  
  101.     #define AssertHandleIsValid(h, msg)                {/*nothing*/}
  102.     #define AssertResourceIsValid(r, msg)            {/*nothing*/}
  103.     #define AssertAddressIsValid(p, msg)            {/*nothing*/}
  104.     #define AssertAddressIsValidAlign(p, a, msg)    {/*nothing*/}
  105.     #define AssertAddressIsValidAlign2( p, msg)        {/*nothing*/}
  106.     #define AssertAddressIsValidAlign4( p, msg)        {/*nothing*/}
  107.     #define AssertNoErr(err, msg)                    {/*nothing*/}
  108.     
  109. #endif    // DEBUG
  110.  
  111.  
  112.  
  113.  
  114.  
  115. /*________________________________________________________________________________
  116.     'USE_DEBUG_TRAPS' controls whether debugging traps are used.
  117.     It can be set before including this file.  Otherwise, it defaults
  118.     to the same status as DEBUG
  119. ________________________________________________________________________________*/
  120. #ifndef USE_DEBUG_TRAPS
  121.     #define USE_DEBUG_TRAPS        ( DEBUG )
  122. #endif
  123.  
  124. #if USE_DEBUG_TRAPS
  125.     #include "DebugTraps.h"
  126. #endif
  127.  
  128.  
  129.  
  130.  
  131. /*________________________________________________________________________________
  132.     'USE_DEBUG_LEAKS' controls whether leaks code compiles in.
  133.     it can be set before #including this file. Otherwise, it defaults
  134.     to the same status as USE_DEBUG_TRAPS.
  135. ________________________________________________________________________________*/
  136. #ifndef USE_DEBUG_LEAKS
  137.     #define USE_DEBUG_LEAKS        ( USE_DEBUG_TRAPS )
  138. #else
  139.     // USE_DEBUG_LEAKS must be off if USE_DEBUG_TRAPS is off
  140.     #if ! USE_DEBUG_TRAPS
  141.         #undef USE_DEBUG_LEAKS
  142.         #define USE_DEBUG_LEAKS    0
  143.     #endif
  144. #endif
  145.  
  146. #include "DebugLeaks.h"
  147.  
  148.  
  149.  
  150. #ifdef __cplusplus
  151. }
  152. #endif
  153.  
  154.  
  155.  
  156.  
  157.